package com.samphanie.auiu.auth.utils;

import com.samphanie.auiu.common.dto.PermissionsDto;
import com.samphanie.auiu.common.dto.RoleDto;
import com.samphanie.auiu.common.dto.UserDto;
import com.samphanie.auiu.mbg.model.SysRole;
import com.samphanie.auiu.mbg.model.User;

import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author ZSY
 * @email 1451691457@qq.com
 */
public class UserUtil {

    public static Function<User, UserDto> fromUser = (user) -> UserDto.builder()
            .id(user.getId())
            .username(user.getUsername())
            .nickname(user.getNickname())
            .realName(user.getRealName())
            .email(user.getEmail())
            .mobile(user.getMobile())
            .gender(user.getGender())
            .birthday(user.getBirthday())
            .avatar(user.getAvatar())
            .city(user.getCity())
            .note(user.getNote())
            .registerIp(user.getRegisterIp())
            .registerAddress(user.getRegisterAddress())
            .enabled(user.isEnabled())
            .accountNonExpired(user.isAccountNonExpired())
            .accountNonLocked(user.isAccountNonLocked())
            .credentialsNonExpired(user.isCredentialsNonExpired())
            .usingMfa(user.isUsingMfa())
            .roles(user.getRoles().stream()
                    .map(role -> RoleDto.builder()
                            .id(role.getId())
                            .roleName(role.getRoleName())
                            .displayName(role.getDisplayName())
                            .builtIn(role.isBuiltIn())
                            .build())
                    .collect(Collectors.toSet()))
            .build();

    public static Function<SysRole, RoleDto> fromRole = (role) -> RoleDto.builder()
            .id(role.getId())
            .roleName(role.getRoleName())
            .displayName(role.getDisplayName())
            .builtIn(role.isBuiltIn())
            .permissions(role.getPermissions().stream()
                    .map(permissions -> PermissionsDto.builder()
                            .id(permissions.getId())
                            .authority(permissions.getAuthority())
                            .displayName(permissions.getDisplayName())
                            .build()
                    ).collect(Collectors.toSet())
            )
            .build();

}
